package com.vids.task;

import com.vids.alarm.MyRejected;
import com.vids.mapper.ImageComparedDetailMapper;
import com.vids.mapper.ImageComparedSummaryMapper;
import com.vids.utils.AlarmUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

//@RequiredArgsConstructor

@Component
@Slf4j
public class ImageComparedTaskSchedule
{
    @Resource
    private ImageComparedSummaryMapper imageComparedSummaryMapper;

    @Resource
    private ImageComparedDetailMapper imageComparedDetailMapper;

    // 线程池
    private ThreadPoolExecutor pool;

    private int MaxThreadNum = 10;

    public static HashMap<String, String> apeIdTaskMap = null;

    public static Boolean isLoaded = false;

    public ImageComparedTaskSchedule()
    {
        try {
            pool = new ThreadPoolExecutor(20, 40, 60, TimeUnit.SECONDS,
                                          new LinkedBlockingQueue<Runnable>(), new MyRejected());
            if (!isLoaded)
            {
                apeIdTaskMap = AlarmUtils.loadApeId2IpAddrMapJson();
                isLoaded = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Scheduled(cron = "0 0 0 * * ?")
    public void clearViidOldFilesTask()
    {
         String  viid_root = "/mount/data1/viid_picture/";

        RemoveFilesTask viid_task = new RemoveFilesTask(viid_root);
        pool.execute(viid_task);
        log.info("viid_root removed "+ viid_root);
    }

    @Scheduled(cron = "0 5 0 * * ?")
    public void clearApeOldFilesTask()
    {
        String  ape_root = "/mount/data1/picture/";

        RemoveFilesTask ape_task = new RemoveFilesTask(ape_root);
        pool.execute(ape_task);
        log.info("ape_root removed "+ ape_root);
    }


//    @Scheduled(cron = "0 5 * * * ?")
//    public void copyFilesTask()
//    {
//        String  viid_root = "/mount/data1/viid_picture/";
//        String  ape_root = "/mount/data1/picture/";
//
//        CopyFilesTask viid_task = new CopyFilesTask(ape_root,viid_root);
//
//        pool.execute(viid_task);
//        log.info("Copy file from ape dir: {} to viid dir: {}", ape_root, viid_root);
//    }


/*
  1、将所有比对任务平均分配给20个线程，每个线程对分配的apeId，做图片比对，并将比对结果入库。
  2、比对功能（将t_ape表中每个摄像头的采集的图片存储在picture目录下，从其他视图平台订阅的图片存储在viid_picture目录下）
   (1)先根据系统时间，确定应该比较的目录（日期+几点）,具体算法是当前系统时间减去一个小时，再取日期和小时，组成路径。
   (2)再依次遍历face、person、motor等类型目录，并确定其下的apeId列表。
   (3)将picture目录和viid_picture目录下同名的apeId目录中图片名称中的时间戳进行比对，并将比对结果记录到detail和summary表中。

   /mount/data1/viid_picture/2024-06-06/13/face
   /mount/data1/picture/2024-06-06/13/face
* */

//  每小时过10分钟触发一次人脸比对
    @Scheduled(cron = "0 5 * * * ?")
    public void ImageComparedFaceTask()
    {
//        for (int i = 0; i < MaxThreadNum; i++) {
            log.info("ImageComparedTask face Thread created and run");
            ImageComparedTask task = new ImageComparedTask(apeIdTaskMap, 0, 1, "face");
            task.setImageComparedDetailMapper(imageComparedDetailMapper);
            task.setImageComparedSummaryMapper(imageComparedSummaryMapper);
            pool.execute(task);
//        }
    }

//   每小时过20分钟触发一次人体比对
    @Scheduled(cron = "0 15 * * * ?")
    public void ImageComparedPersonTask()
    {
//        for (int i=0; i < MaxThreadNum; i++) {
            log.info("ImageComparedTask person Thread created and run");
            ImageComparedTask task = new ImageComparedTask(apeIdTaskMap, 0, 1, "person");
            task.setImageComparedDetailMapper(imageComparedDetailMapper);
            task.setImageComparedSummaryMapper(imageComparedSummaryMapper);
            pool.execute(task);
//        }
    }


//  每小时过30分钟触发一次机动车比对
    @Scheduled(cron = "0 20 * * * ?")
    public void ImageComparedMotorTask()
    {
//        for (int i = 0; i < MaxThreadNum; i++)
//        {
//            log.info("ImageComparedTask motor Thread"+i+" created and run");
            log.info("ImageComparedTask motor Thread created and run");
            ImageComparedTask task = new ImageComparedTask(apeIdTaskMap, 0, 1, "motor");
            task.setImageComparedDetailMapper(imageComparedDetailMapper);
            task.setImageComparedSummaryMapper(imageComparedSummaryMapper);
            pool.execute(task);
//        }
    }

//    每小时过40分钟触发一次非机动车比对
    @Scheduled(cron = "0 30 * * * ?")
    public void ImageComparedNonMotorTask()
    {
//        for (int i= 0; i < MaxThreadNum; i++) {
            log.info("ImageComparedTask non_motor Thread created and run");
//            log.info("ImageComparedTask non_motor Thread"+i+" created and run");
            ImageComparedTask non_motor_task = new ImageComparedTask(apeIdTaskMap, 0, 1,"non_motor");
            non_motor_task.setImageComparedDetailMapper(imageComparedDetailMapper);
            non_motor_task.setImageComparedSummaryMapper(imageComparedSummaryMapper);
            pool.execute(non_motor_task);
//        }
    }


}
